home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / qbsnip.zip / EXEPATH.BAS < prev    next >
BASIC Source File  |  1997-05-04  |  2KB  |  60 lines

  1. ' EXECPATH.BAS
  2. ' by Joe Negron
  3. '
  4. ' public domain
  5. ' No warranties or guarantees are expressed or implied.
  6. '
  7. ' Purpose: Gets the path and filename of the executable running.
  8. '
  9. 'Function Description:
  10. '
  11. 'Uses DOS ISR 21H, Function 51H (Get PSP Address) to return the
  12. 'name of the currently executing program.  Note that this FUNCTION
  13. 'requires DOS 3.0 or >.
  14.  
  15. DECLARE FUNCTION ExePath$ ()
  16. DEFINT A-Z
  17. '$INCLUDE: 'qb.bi'
  18.  
  19. E$ = ExePath$
  20. PRINT E$
  21.  
  22. FUNCTION ExePath$ STATIC
  23.      DIM Regs AS RegType                       'Allocate space for TYPE
  24.                                                                                          '  RegType
  25.      Regs.ax = &H5100                          'DOS function 51h
  26.      INTERRUPT &H21, Regs, Regs                '  Get PSP Address
  27.  
  28.      DEF SEG = Regs.bx                         'Regs.bx returns PSP sgmnt.
  29.      EnvSeg% = PEEK(&H2C) + PEEK(&H2D) * 256   'Get environment address
  30.      DEF SEG = EnvSeg%                         'Set environment address
  31.  
  32.      DO
  33.             Byte% = PEEK(Offset%)                  'Take a byte
  34.  
  35.             IF Byte% = 0 THEN                      'Items are ASCIIZ
  36.                  Count% = Count% + 1                 '  terminated
  37.  
  38.                  IF Count% AND EXEFlag% THEN         'EXE also ASCIIZ terminated
  39.                         EXIT DO                          'Exit at the end
  40.                  ELSEIF Count% = 2 THEN              'Last entry in env. is
  41.                         EXEFlag% = -1                    '  terminated with two
  42.                         Offset% = Offset% + 2            '  NULs.  Two bytes ahead
  43.                  END IF                              '  is the EXE file name.
  44.             ELSE                                   'If Byte% <> 0, reset
  45.                  Count% = 0                          '  zero counter
  46.  
  47.                  IF EXEFlag% THEN                    'If EXE name found,
  48.                         Temp$ = Temp$ + CHR$(Byte%)      '  build string
  49.                  END IF
  50.             END IF
  51.  
  52.             Offset% = Offset% + 1                  'To grab next byte...
  53.      LOOP                                      'Do it again
  54.  
  55.      DEF SEG                                   'Reset default segment
  56.      ExePath$ = Temp$                          'Return value
  57.      Temp$ = ""                                'Clean up
  58. END FUNCTION
  59.  
  60.